package com.flycent.hqhs.ui;

import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.flycent.hqhs.R;
import com.flycent.hqhs.bean.ProductCategory;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.http.WebException;
import com.flycent.hqhs.ui.view.SimpleTitleBar;
import com.flycent.hqhs.util.ContextUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 分类
 * @author fbl
 */
public class ClassifyActivity extends SimpleTitleBarActivity {
	@ViewInject(id = R.id.lv, itemClick="onItemClick") ListView lv;
	@ViewInject(id = R.id.gv, itemClick="onItemClick") GridView gv;
	@ViewInject(id = R.id.classfiy_search_edit, click = "butClick") EditText mEdit;

	private List<ProductCategory> categorys;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_classify);
		FinalActivity.initInjectedView(this);

		// 左列，及第一个分类的详情（右列）
		new AsyncTask<Void, Void, Void>() {

			@Override
			protected void onPreExecute() {
				showLoadingDialog("",false);
			};

			@Override
			protected Void doInBackground(Void... params) {
				try {
					categorys = WebClient.getInstance(ClassifyActivity.this).productCategorys();
				} catch (WebException e) {
					dismissLoadingDialog();
					e.printStackTrace();
				}catch (Exception e) {
					dismissLoadingDialog();
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				if (null != categorys) {
					lv.setAdapter(new ArrayAdapter<>(ClassifyActivity.this, R.layout.list_item_classify, categorys));
					lv.setItemChecked(lastChecked, true);

					// 右列
					if (0 != categorys.size()) gv.setAdapter(new CategoryAdapter(0));
				}

				dismissLoadingDialog();
			};
		}.execute();

	}

	public int lastChecked = 0;
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		switch (parent.getId()) {
		case R.id.lv: // list
			if (lastChecked != position) {
				lastChecked = position;

				gv.setAdapter(new CategoryAdapter(position));
			}

			break;
		case R.id.gv: // grid
			startActivity(new Intent(this, SearchListActivity.class)
			.putExtra("CATEGORY_ID", ((ProductCategory) gv.getItemAtPosition(position)).getId())
			.putExtra("CATEGORY_NAME", ((ProductCategory) gv.getItemAtPosition(position)).getName())
			.putExtra("APP_LAYOUT", ((ProductCategory) gv.getItemAtPosition(position)).getAppLayout() + ""));

			break;

		default:
			break;
		}
	}

	// grid adapter
	private class CategoryAdapter extends ArrayAdapter<ProductCategory> {
		private ImageLoader loader = ImageLoader.getInstance();

		/** @param position 左列中的position */
		public CategoryAdapter(int position) {
			super(ClassifyActivity.this, R.layout.grid_item_classify, android.R.id.text1, categorys.get(position).getChildren());
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = super.getView(position, convertView, parent);

			loader.displayImage(getItem(position).getAppImage(), (ImageView) v.findViewById(android.R.id.icon));
			return v;
		}
	}
	public void butClick(View v){
		if( v == mEdit){
			ContextUtil.startActivity(this, SearchActivity.class);
		}
	}


	@Override
	protected void onTitleBarCreated(SimpleTitleBar titleBar) {
		super.onTitleBarCreated(titleBar);
		titleBar.setTitle(R.string.main_classify);
	}

}
